value multiplied by itself exponent times - power(2, 3) is 8 and power(10, 2) is 100. The exponent has to be a whole number; power(2, 0.5) is refused when the project is checked. An answer bigger than about 524,000 goes wrong.
Sega Genesis: not built yet
On other consoles this differs: see Side by side and How the consoles differ.
power(value, exponent) -> real| Name | Type |
|---|---|
value | real |
exponent | int |
real
value multiplied by itself exponent times. power(2, 3) is 8 (2 times 2 times 2) and power(10, 2) is 100.
The exponent has to be a whole number. power(2, 3) is fine. power(2, 2.5) is refused when the project is checked, and so is a variable that holds a decimal, even a decimal like 3.0. This is on purpose: a power with a fraction in it is really a root (power(9, 0.5) would be the square root of 9), and the only root GBML has is sqrt. If you have a decimal that you know holds a whole number, int_part hands it over as one.
power hands back a real, even when both numbers are whole. If the number is counted and should stay a whole number - 10 to the power of the number of digits in a score, say - use power_int instead; it also goes up to about 2.1 billion, where power runs out at about 524,000.
var level = 4;
var frames_to_grow = 30 * power(2, level);Each level takes twice as long as the one before: power(2, 4) is 16, so frames_to_grow is 480.
| You write | You get | Why |
|---|---|---|
power(5, 0) | 1 | anything to the power of nothing is 1 |
power(0, 0) | 1 | the usual rule, so power(x, 0) is 1 for every x |
power(-2, 3) | -8 | a minus number is fine; an odd exponent keeps the minus, an even one (power(-2, 2) is 4) drops it |
power(2, -1) | 0.5 | a minus exponent is one divided by the power: 1 / power(2, 1) |
power(0, -1) | 0 | one divided by nothing has no answer, so it gives 0 instead of stopping the game (the way sqrt answers 0 for a minus number) |
power(0.5, -13) | 8192 | a number between -1 and 1 is flipped first (1 / 0.5 is 2, then power(2, 13)), so a tiny power is not rounded away to nothing on the way |
A real tops out near 524,288. An answer of 524,288 or more goes wrong - there is no error and no warning, the answer simply wraps round. power(2, 18) is 262,144 and fits. power(2, 19) should be 524,288, and comes out as -524,288. power(10, 5) is 100,000, but power(10, 6) should be 1,000,000 and is -48,576 instead.
Anything under that edge is right. A bigger exponent on its own is fine: power(2, 1000000000) is answered at once (the work grows with how many binary digits the exponent has, not with its size), though the answer to that one is of course far past the edge.
Every multiply is exact to one 1/4096 step, so a long chain of them drifts a little. power(1.1, 10) is 2.5872, where the true answer is 2.5937. Part of that is 1.1 itself, which a real can only hold as 1.0998. Whole numbers do not have the problem: power(3, 5) is exactly 243.
About one multiply for every binary digit of the exponent - power(x, 10) is three squarings and two more multiplies, not ten of them. Measured at about 350 cycles for power(x, 3) and about 540 for power(x, 10), counting the loop around the call (a frame is 280,896 cycles, so the dearer one is about two tenths of one percent of a frame). A minus exponent adds one division, about 230 cycles more. Cheap enough for every frame.
The exponent is an int, not a real, on purpose: a fractional power is a root, and this engine’s only root is sqrt (good to 1/64). It works by square-and-multiply, so even power(2, 1000000000) is a few dozen multiplies, never a frozen console. exponent 0 gives 1 (power(0, 0) is 1 too). A negative value is fine: power(-2, 3) is -8. A negative exponent is one over the positive power: power(2, -1) is 0.5; power(0, -1) has no answer and gives 0, and a value between -1 and 1 is flipped first, so power(0.5, -13) is 8192, not 0. Each multiply is exact to one 1/4096, so a long chain drifts a few steps: power(1.1, 10) is 2.5872, true 2.5937. The ceiling is real’s own: an answer of 524,288 or more (or past -524,288) comes out wrong, silently, wrapping round: power(2, 18) is 262,144 but power(2, 19) is -524,288. It hands back a real, even for whole numbers: for something counted, such as 10 to the power of a number of digits, power_int keeps the answer a whole number and goes up to about 2.1 billion.